const config = require('../../../../../ajax.js');
let app = getApp()
let teacherInfo = app.globalData.teacherInfo
let storageTeacherInfo;
let unSignInId = []   //手动签到的学生的Id合集
Page({
  data: {
    indicatorDots: false,
    autoplay: false,
    interval: 1000,
    duration: 300,
    tapList: [
      {
        "id": 0,
        "name": "已签到"
      },
      {
        "id": 1,
        "name": "未签到"
      },
      {
        "id": 2,
        "name": "请假"
      }
    ],
    tabIndex: 0,
    indexId: 0,
    notSignIn: null,
    showCheckBtn: false, // 是否显示签到按钮(数据为空或未选中签到人数时为false)
    defalut: '../../image/signIn/bg2.png'
  },

  onLoad: function () {
    let that = this
    wx.showLoading({
      title: '加载中',
    })
    wx.getStorage({
      key: 'teacherInfo',
      success: function (res) {
        storageTeacherInfo = res.data
        config.ajax('POST', {
          "classId": "e6146dd8a9cc4e5e8d19c1f3f855b8c2",
          "staffId": storageTeacherInfo.staffId,
          "organizationId": storageTeacherInfo.organizationId,
          "signInType": "AFTERNOON_SIGN_IN"
        }, '/teacher/faceApi/getStudentListByClassId', (res) => {
          wx.hideLoading()
          if (res.statusCode === 200) {
            let pickedList = res.data.data
            if (pickedList.length <= 0) {
              wx.showToast({
                title: '没有已签到的人',
                icon: 'none'
              })
            }
            that.setData({
              pickedList
            })
          }
          else {
            wx.showModal({
              title: '警告',
              content: '出错啦，请稍后再试',
              success(res) {
                if (res.confirm) {
                  wx.navigateBack()
                }
              }
            })
          }
        })
      },
    })
   
  },

  tapList: function (e) {
    let indexId = e.currentTarget.dataset.id;
    let tabValue = e._relatedInfo.anchorRelatedText;
    let tabIndex;
    switch (tabValue) {
      case "已签到":
        tabIndex = 0
        break;
      case "未签到":
        tabIndex = 1
        break;
      case "请假":
        tabIndex = 2
        break;
    }
    this.setData({
      tabIndex: tabIndex,
      indexId: indexId
    })
  },

  tapTap: function (e) {
    let that = this
    let tapIndex = e.detail.current
    switch (tapIndex) {
      case 0:
        wx.showLoading({
          title: '加载中',
        })
        config.ajax('POST', {
          "classId": "e6146dd8a9cc4e5e8d19c1f3f855b8c2",
          "staffId": storageTeacherInfo.staffId,
          "organizationId": storageTeacherInfo.organizationId,
          "signInType": "AFTERNOON_SIGN_IN"
        }, '/teacher/faceApi/getStudentListByClassId', (res) => {
          wx.hideLoading()
          if (res.statusCode === 200) {
            let pickedList = res.data.data
            if (pickedList.length <= 0) {
              wx.showToast({
                title: '没有已签到的人',
                icon: 'none'
              })
            }
            that.setData({
              pickedList
            })
          }
          else {
            wx.showModal({
              title: '警告',
              content: '出错啦，请稍后再试',
              success(res) {
                if (res.confirm) {
                  wx.navigateBack()
                }
              }
            })
          }
        })
        break;
      case 1:
        wx.showLoading({
          title: '加载中',
        })
        config.ajax('POST', {
          "classId": "e6146dd8a9cc4e5e8d19c1f3f855b8c2",
          "staffId": storageTeacherInfo.staffId,
          "organizationId": storageTeacherInfo.organizationId,
          "signInType": "AFTERNOON_NOT_SIGN_IN"
        }, '/teacher/faceApi/getStudentListByClassId', (res) => {
          wx.hideLoading()
          if (res.statusCode === 200) {
            let notSignIn = res.data.data
            let showCheckBtn = false;
            if (notSignIn.length > 0) {
              for (let i = 0; i < notSignIn.length; i++) {
                notSignIn[i].isChange = false
              }
            }
            else {
              wx.showToast({
                title: '没有未签到的人',
                icon: 'none'
              })
            }

            that.setData({
              notSignIn,
              showCheckBtn
            })
          }
          else {
            wx.showModal({
              title: '警告',
              content: '出错啦，请稍后再试',
              success(res) {
                if (res.confirm) {
                  wx.navigateBack()
                }
              }
            })
          }
        })
        break;
      case 2:
        wx.showLoading({
          title: '加载中',
        })
        config.ajax('POST', {
          "classId": "e6146dd8a9cc4e5e8d19c1f3f855b8c2",
          "staffId": storageTeacherInfo.staffId,
          "organizationId": storageTeacherInfo.organizationId,
          "signInType": "LEAVE"
        }, '/teacher/faceApi/getStudentListByClassId', (res) => {
          wx.hideLoading()
          if (res.statusCode === 200) {
            let leaveList = res.data.data
            if ( leaveList.length <=0 ) {
              wx.showToast({
                title: '没有请假的人',
                icon: 'none'
              })
            }
            that.setData({
              leaveList
            })
          }
          else {
            wx.showModal({
              title: '警告',
              content: '出错啦，请稍后再试',
              success(res) {
                if (res.confirm) {
                  wx.navigateBack()
                }
              }
            })
          }
        })
        break
    }
    that.setData({
      indexId: e.detail.current
    })
  },

  chooseItem: function (e) {
    let that = this
    let notSignIn = that.data.notSignIn
    let changedId = e.currentTarget.dataset.id
    let index = unSignInId.indexOf(changedId)

    switch (index) {
      case -1:
        unSignInId.push(changedId)
        break;
      default:
        unSignInId.splice(index, 1)
    }
    //选中状态
    let array = [];
    let key = null;
    for (let i = 0; i < notSignIn.length; i++) {
      array.push(notSignIn[i].id)
      if (changedId == array[i]) {
        key = array.indexOf(changedId)
      }
    }
    notSignIn[key].isChange = !notSignIn[key].isChange

    that.setData({
      notSignIn: notSignIn,
      showCheckBtn: unSignInId.length > 0 ? true : false
    })
  },

  handleClickSignIn: function () {
    let that = this
    wx.showActionSheet({
      itemList: ['学校门口签到', '托辅门口签到'],
      success(res) {
        // console.log('===>>>', res.tapIndex)
        // todo
        that.handleSignIn()
      }
    })
  },

  handleSignIn: function () {
    let that = this
    wx.showLoading({
      title: '签到中',
    })
    config.ajax('POST', {
      "staffId": storageTeacherInfo.staffId,
      "organizationId": storageTeacherInfo.organizationId,
      "studentIds": unSignInId
    }, '/teacher/faceApi/addSignInRecordByTeacher', (res) => {
      if (res.data.status == 200) {
        unSignInId = []  //清空数组
        wx.hideLoading()
        wx.showToast({
          title: '签到成功',
          icon: 'success',
          duration: 2000
        })

        // 添加成功刷新页面
        config.ajax('POST', {
          "classId": "e6146dd8a9cc4e5e8d19c1f3f855b8c2",
          "staffId": storageTeacherInfo.staffId,
          "organizationId": storageTeacherInfo.organizationId,
          "signInType": "AFTERNOON_NOT_SIGN_IN"
        }, '/teacher/faceApi/getStudentListByClassId', (res) => {
          let notSignIn = res.data.data
          let showCheckBtn = false;
          if (notSignIn.length > 0) {
            for (let i = 0; i < notSignIn.length; i++) {
              notSignIn[i].isChange = false
            }
          }
          else {
            wx.showToast({
              title: '全部签到啦'
            })
          }

          that.setData({
            notSignIn,
            showCheckBtn
          }) 
        })
      }
      else {
        wx.hideLoading()
        wx.showModal({
          title: '系统提示',
          content: '出错啦，请稍后再试',
          success(res) {
            // todo
            // 刷新页面？
          }
        })
      }
    })
  }
})